﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Data;
using System.Collections;

using CE.Global;
using CE.User;
using CE.Database;
using CE.Security;

public partial class home : MyLanguageBasePage
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Master.Page.Title = Resources.Title.HomeTitle;

        LiteralControl newln = new LiteralControl(Environment.NewLine);
        HtmlMeta language = new HtmlMeta();
        language.Name = "language";
        language.Content = Resources.Title.PageLanguage;

        HtmlMeta keywords = new HtmlMeta();
        keywords.Name = "keywords";
        keywords.Content = Resources.Title.HomeKeyword;

        HtmlMeta description = new HtmlMeta();
        description.Name = "description";
        description.Content = Resources.Title.HomeDescription;

        Page.Header.Controls.AddAt(10, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(11, language);
        Page.Header.Controls.AddAt(12, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(13, keywords);
        Page.Header.Controls.AddAt(14, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(15, description);

        if (!IsPostBack)
        {

            //CE.Email.Welcome.SendMail("sakthivel.kandasamy@teledirect.com.my", "GG Tester", "testing");
            //CE.Email.Welcome.SendMail("kandasamy.sakthivel@comparexpress.com", "GG Tester", "testing");
            txtEmail.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtEmail.ClientID + "','" + rfvEmail.ClientID + "','" + revEmail.ClientID + "');");
            divSubscribe.Visible = true;
            divThankYou.Visible = false;

            hplHowToBuy.NavigateUrl = CE.Global.General.GetUrl("/faq/how-to-buy-car-insurance", Page);
            hplHowToBuyReadMore.NavigateUrl = CE.Global.General.GetUrl("/faq/how-to-buy-car-insurance", Page);
            hplCreateReminder.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/set-insurance-reminder", Page);
            hplCreateReminderReadMore.NavigateUrl = CE.Global.General.GetUrl("/car-insurance/set-insurance-reminder", Page);
            hplAllPartners.NavigateUrl = CE.Global.General.GetUrl("/partners/", Page);

            string _PromoUrl = CE.Global.General.GetUrl("/promotion/promo-call-me", Page);
            hplNewPromo1.NavigateUrl = _PromoUrl + "?type=1";
            hplNewPromo2.NavigateUrl = _PromoUrl + "?type=2";
            hplNewPromo3.NavigateUrl = _PromoUrl + "?type=3";
            hplNewPromo4.NavigateUrl = _PromoUrl + "?type=4";
            hplNewPromo5.NavigateUrl = _PromoUrl + "?type=5";
            hplNewPromo6.NavigateUrl = _PromoUrl + "?type=6";
            hplNewPromo7.NavigateUrl = _PromoUrl + "?type=7";
            hplNewPromo8.NavigateUrl = _PromoUrl + "?type=8";
            hplNewPromo9.NavigateUrl = _PromoUrl + "?type=9";
            hplNewPromoCar1.NavigateUrl = _PromoUrl + "?type=1";
            hplNewPromoCar2.NavigateUrl = _PromoUrl + "?type=2";
            hplNewPromoCar3.NavigateUrl = _PromoUrl + "?type=3";
            hplNewPromoCar4.NavigateUrl = _PromoUrl + "?type=4";
            hplNewPromoCar5.NavigateUrl = _PromoUrl + "?type=5";
            hplNewPromoCar6.NavigateUrl = _PromoUrl + "?type=6";
            hplNewPromoCar7.NavigateUrl = _PromoUrl + "?type=7";
            hplNewPromoCar8.NavigateUrl = _PromoUrl + "?type=8";
            hplNewPromoCar9.NavigateUrl = _PromoUrl + "?type=9";
            btnCallforDiscount8.PostBackUrl = _PromoUrl + "?type=3";
            btnCallforDiscount9.PostBackUrl = _PromoUrl + "?type=5";
            btnCallforDiscount10.PostBackUrl = _PromoUrl + "?type=10";

           
            btnCallforDiscount1.PostBackUrl = CE.Global.General.GetUrl("/car-insurance/call-me", Page);
            btnCallforDiscount2.PostBackUrl = CE.Global.General.GetUrl("/car-insurance/call-me", Page);
            btnCallforDiscount3.PostBackUrl = CE.Global.General.GetUrl("/car-insurance/call-me", Page);
            btnCallforDiscount4.PostBackUrl = CE.Global.General.GetUrl("/car-insurance/call-me", Page);
            btnCallforDiscount5.PostBackUrl = CE.Global.General.GetUrl("/car-insurance/call-me", Page);

            aTestimonial1.HRef = CE.Global.General.GetUrl("/customer-testimonial/testimonial", Page);
            aTestimonial2.HRef = CE.Global.General.GetUrl("/customer-testimonial/testimonial", Page);
            aTestimonial3.HRef = CE.Global.General.GetUrl("/customer-testimonial/testimonial", Page);

            #region Promotion Slider Display
            PromoDisplay();
            #endregion
        }
    }

    protected void btnGetQuote_Click(object sender, EventArgs e)
    {
        try
        {
            string _strCarRedirectURL = CE.Global.General.GetUrl("/car-insurance/car-details?type=hm", Page);
            bool _isBuyComplete = false;
            HttpCookie _cookie = Request.Cookies.Get("GGQuote");
            if (_cookie != null)
            {
                Account _useraccount = new Account();
                AccountDB _useraccountdb = new AccountDB();
                _useraccount = _useraccountdb.GetUserAccount(MySecurity.Decrypt(_cookie.Values.Get("CustomerEmail"), true), Convert.ToInt32(CE.Global.CEUserType.Customer));
                if (_useraccount != null)
                {
                    Int64 CEUserId = _useraccount.CEID;
                    Int64 _result = -1;
                    MyDatabase _db = new MyDatabase();
                    DataTable _dtResult = new DataTable();
                    string _spName = "sprocCheckUserBuyingStatus";
                    SortedList _sPara = new SortedList();
                    _sPara.Add("@RefCEUserId", CEUserId);

                    _result = _db.ExecuteNonSqlSP(_spName, _sPara);
                    if (_result > 0)
                        _isBuyComplete = true;
                }
            }
            if (_isBuyComplete)
            {
                _strCarRedirectURL = CE.Global.General.GetUrl("/car-insurance/car-details?type=hm", Page);
            }
            else
            {
                string _randomUrl = GetCarQuoteUrl();
                _strCarRedirectURL = CE.Global.General.GetUrl(_randomUrl, Page);
                _strCarRedirectURL = CE.Global.General.GetUrl(_randomUrl, Page);
            }
            Response.Redirect(_strCarRedirectURL);
        }
        catch (Exception ex)
        {

        }
    }

    protected void btnLMGQuote_Click(object sender, EventArgs e)
    {
        try
        {
            string _strCarRedirectURL = CE.Global.General.GetUrl("/car-insurance/promo-car-quote", Page);
            Response.Redirect(_strCarRedirectURL);
        }
        catch (Exception ex)
        {

        }
    }

    protected void btnGetQuoteTU_Click(object sender, EventArgs e)
    {
        try
        {
            string _strCarRedirectURL = CE.Global.General.GetUrl("/car-insurance/trueyou-car-quote", Page);            
            Response.Redirect(_strCarRedirectURL);
        }
        catch (Exception ex)
        {

        }
    }

    protected void btnSubscribe_Click(object sender, EventArgs e)
    {
        try
        {
            ChangeValidationEffectServerSide("subscriber");
            Page.Validate("subscriber");
            if (Page.IsValid)
            {
                UserSubscriber _usersub = new UserSubscriber();
                UserSubscriberDB _usersubdb = new UserSubscriberDB();
                _usersub.Email = txtEmail.Text;
                if (_usersubdb.AlreadyExist(_usersub.Email) > 0)
                {
                    divSubscribe.Visible = false;
                    divThankYou.Visible = true;
                    lblThankYou.Text = "";
                    lblThankYou.Visible = false;
                    lblExist.Visible = true;
                }
                else if (_usersubdb.Insert(_usersub) > 0)
                {
                    divSubscribe.Visible = false;
                    divThankYou.Visible = true;
                    lblExist.Text = "";
                    lblThankYou.Visible = true;
                    lblExist.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        txtEmail.Text = HttpUtility.HtmlEncode(txtEmail.Text);

        //Email
        if (rfvEmail.Enabled && rfvEmail.ValidationGroup == strValidationGroup)
        {
            rfvEmail.Validate();
            if (rfvEmail.IsValid == false)
            {
                txtEmail.ControlStyle.CssClass = "input-field validationError";
            }
            else
            {
                txtEmail.ControlStyle.CssClass = "input-field";
            }
        }
        else
        {
            txtEmail.ControlStyle.CssClass = "input-field";
        }

        if (revEmail.Enabled && revEmail.ValidationGroup == strValidationGroup)
        {
            if (rfvEmail.IsValid)
            {
                revEmail.Validate();
                if (revEmail.IsValid == false)
                {
                    txtEmail.ControlStyle.CssClass = "input-field validationError";
                }
                else
                {
                    txtEmail.ControlStyle.CssClass = "input-field";
                }
            }
        }
        else
        {
            txtEmail.ControlStyle.CssClass = "input-field";
        }
    }

    public string GetCarQuoteUrl()
    {
        #region Promotion Status
        bool _IsPromo = false;
        string _Promo = CE.Global.General.GetUTCDateTime().ToString("yyyyMMdd");
        Int64 _intPromo = 0;
        if (!Int64.TryParse(_Promo, out _intPromo))
        {
            _intPromo = 0;
        }
        if (Session["Language"] != null & Session["Language"].ToString() == "en-US")
        {
            if (_intPromo >= 20150413 && _intPromo <= 20150430)
            {
                _IsPromo = true;
            }
        }
        else if (Session["Language"] != null & Session["Language"].ToString() == "th-TH")
        {
            if (_intPromo >= 25580413 && _intPromo <= 25580430)
            {
                _IsPromo = true;
            }
        }
        #endregion

        string _url = "";
        Int32 _result = 0;
        string[] urls = new string[2];
        urls[0] = "/car-insurance/car-quote?type=hmp";
        urls[1] = "/car-insurance/car-details?type=hm";
        if (_IsPromo)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable();
            string _spName = "sprocSelectRandomNumberFromCarQuote";

            _dtResult = _db.GetDataTableBySP(_spName);
            if (_dtResult != null && _dtResult.Rows.Count > 0)
            {
                if (!Int32.TryParse(_dtResult.Rows[0]["RandomNumber"].ToString(), out _result))
                {
                    _result = 0;
                }
            }
            _url = urls[_result];
        }
        else
        {
            _url = urls[1];
        }
        return _url;
    }

    public void PromoDisplay()
    {
        try
        {
            #region CNY Promotion - Upto 27 Feb 2015
            //string _CNYPromo = CE.Global.General.GetUTCDateTime().ToString("yyyyMMdd");
            //Int64 _intCNYPromo = 0;
            //if (!Int64.TryParse(_CNYPromo, out _intCNYPromo))
            //{
            //    _intCNYPromo = 0;
            //}
            //if (Session["Language"] != null & Session["Language"].ToString() == "en-US")
            //{
            //    if (_intCNYPromo >= 20150202 && _intCNYPromo <= 20150227)
            //    {
            //        divCNYPromotion.Visible = true;
            //    }
            //}
            //else if (Session["Language"] != null & Session["Language"].ToString() == "th-TH")
            //{
            //    if (_intCNYPromo >= 25580202 && _intCNYPromo <= 25580227)
            //    {
            //        divCNYPromotion.Visible = true;
            //    }
            //}
            #endregion

            #region KSK Promotion - Upto 31 Dec 2015
            string _KSKPromo = CE.Global.General.GetUTCDateTime().ToString("yyyyMMdd");
            Int64 _intKSKPromo = 0;
            if (!Int64.TryParse(_KSKPromo, out _intKSKPromo))
            {
                _intKSKPromo = 0;
            }
            if (Session["Language"] != null & Session["Language"].ToString() == "en-US")
            {
                if (_intKSKPromo > 0 && _intKSKPromo <= 20151231)
                {
                    divKSKPromotion.Visible = true;
                }
            }
            else if (Session["Language"] != null & Session["Language"].ToString() == "th-TH")
            {
                if (_intKSKPromo > 0 && _intKSKPromo <= 25581231)
                {
                    divKSKPromotion.Visible = true;
                }
            }
            #endregion

            #region TrueYou Promotion - Upto 31 Oct 2015
            string _TrueYouPromo = CE.Global.General.GetUTCDateTime().ToString("yyyyMMdd");
            Int64 _intTrueYouPromo = 0;
            if (!Int64.TryParse(_TrueYouPromo, out _intTrueYouPromo))
            {
                _intTrueYouPromo = 0;
            }
            if (Session["Language"] != null & Session["Language"].ToString() == "en-US")
            {
                if (_intTrueYouPromo >= 20150814 && _intTrueYouPromo <= 20151031)
                {
                    divTrueYouPromo.Visible = true;
                }
            }
            else if (Session["Language"] != null & Session["Language"].ToString() == "th-TH")
            {
                if (_intTrueYouPromo >= 25580814 && _intTrueYouPromo <= 25581031)
                {
                    divTrueYouPromo.Visible = true;
                }
            }
            #endregion
        }
        catch (Exception ex)
        {
        }
    }
}
